if not pattern then pattern = {} end
if not pattern.ticks then pattern.ticks = {} end

if not pattern.ticks.ice then

pattern.ticks.ice = {}
pattern.ticks.ice.trends = {}

dofile (ScriptsRootPath .. "lib\\ticks.lua")

function pattern.ticks.ice.Init()
	pattern.ticks.ice.params = lib:LoadIni(ticks.patterns_init, "ice", {trend = 0.1, period = 30})
	if not ticks.clean_period
		or ticks.clean_period < pattern.ticks.ice.params.period * 60
	then
		ticks.clean_period = pattern.ticks.ice.params.period * 60
	end
	if processor and processor.log then
		processor:log("pattern.ticks.ice.params", pattern.ticks.ice.params)
	end
end

function pattern.ticks.ice.OnTick(tick)
	if not tick then return end
	if not pattern.ticks.ice.params then pattern.ticks.ice.Init() end
	
	local price, datetime
	local ct, m = ticks:get(tick)
	if ct and tick.time ~= ct.time and #m > 5 then
		local side = 0
		for i, t in ipairs(m) do
			if t.side then
				side = side + 1
			else
				side = side - 1
			end
		end
		if side ~= 0 then
			local pr, volume
			local fm
			for i, t in ipairs(m) do
				if t.side and side > 0 or not t.side and side < 0 then
					if not pr then
						pr = t.price
						if side > 0 then
							volume = t.volume_offer
						else
							volume = t.volume_bid
						end
						fm = true
					end
					if pr ~= t.price
						or side > 0 and (pr ~= t.price_offer or volume ~= t.volume_offer)
						or side < 0 and (pr ~= t.price_bid or volume ~= t.volume_bid)
					then
						fm = nil
						break
					end
				end
			end
			--message("ice "
			--	.. ticks.clean_period
			--	.. " "
			--	.. int(lib:tickseconds() - lib:tickseconds(ticks.data[tick.sec_code].times[1])))
			if fm then
				price = pr
				datetime = lib:tickdate(tick.time)
				datetime.is_long__ = side < 0
				
				if not pattern.ticks.ice.trends[tick.sec_code] then pattern.ticks.ice.trends[tick.sec_code] = {} end
				if not Settings then
					local hint
					ticks:GetTrend(tick, pattern.ticks.ice.trends[tick.sec_code], pattern.ticks.ice.params.period)

					if side < 0 then	-- buy order
						hint = string.format("ICE BID %s %s %s%%",
							lib:FormatPrice(price),
							lib:bool2side(pattern.ticks.ice.trends[tick.sec_code].is_long),
							tostring(pattern.ticks.ice.trends[tick.sec_code].percent)
						)
					else		-- sell order
						hint = string.format("ICE OFFER %s %s %s%%",
							lib:FormatPrice(price),
							lib:bool2side(pattern.ticks.ice.trends[tick.sec_code].is_long),
							tostring(pattern.ticks.ice.trends[tick.sec_code].percent)
						)
					end
					local notif = tick.sec_code .. " " .. hint
					if processor and processor.log then
						processor:log(notif .. " " .. lib:toline(pattern.ticks.ice.trends[tick.sec_code]))
					end
					
					if pattern.ticks.ice.trends[tick.sec_code].percent
						and (pattern.ticks.ice.trends[tick.sec_code].percent < pattern.ticks.ice.params.trend
					)
					then return end
					
					if pattern.ticks.ice.trends[tick.sec_code].pmax and pattern.ticks.ice.trends[tick.sec_code].pmin then
						pr = pattern.ticks.ice.trends[tick.sec_code].pmax - pattern.ticks.ice.trends[tick.sec_code].pmin
					else
						pr = price * 0.01
					end

					if side < 0 then	-- buy order
						datetime.labels__ = {
							lm = price,
							sl = price + pr / 4,
							tp = price - pr * 3 / 4
						}
					else		-- sell order
						datetime.labels__ = {
							lm = price,
							sl = price - pr / 4,
							tp = price + pr * 3 / 4
						}
					end
					datetime.sec_code__ = tick.sec_code
					datetime.hint__ = hint
					datetime.notify__ = notif
				end
				
				return price, datetime
			end
		end
	end
end

end -- of file


